package cn.dnui_yxk801.dnuifood_16110100801.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.CollectInfoBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.controller.FoodInfoActivity;
import cn.dnui_yxk801.dnuifood_16110100801.controller.FoodListActivity;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.UserModel;

import static cn.dnui_yxk801.dnuifood_16110100801.MainActivity.userid;

public class CollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnRetrofitListener<ResultBean>{
    private Context context;
    private String shop_id;
    private String food_id;
    private List<CollectInfoBean> datalist;
    private LayoutInflater layoutInflater;
    private static String Baseurl="http://172.24.10.175:8080/foodService/";

    public CollectAdapter(Context context,List datalist){
        this.context=context;
        this.datalist=datalist;
        layoutInflater=LayoutInflater.from(context);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=layoutInflater.from(viewGroup.getContext()).inflate(R.layout.collectitem,viewGroup,false);
        return new CollectAdapter.ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int i) {
       final CollectInfoBean entity=(CollectInfoBean)datalist.get(i);
        CollectAdapter.ViewHolder viewHolder=(CollectAdapter.ViewHolder)holder;
        if (entity==null)
            return;
        if (entity.getFlag().equals("0")){
            ;
            viewHolder.ct_name.setText(entity.getShopname());
            viewHolder.ct_name2.setText(entity.getAddress());
            Picasso.with(context).load(Baseurl+entity.getPic()).into(viewHolder.ct_pic);

        viewHolder.collect_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, FoodListActivity.class);
                String shopid=datalist.get(i).getShop_id();
                String shopname=datalist.get(i).getShopname();
                intent.putExtra("shopid",shopid);
                intent.putExtra("shopname",shopname);
                context.startActivity(intent);
            }
        });

        viewHolder.collect_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                uncollectShop(entity.getShop_id());
            }
        });
    }
    else {
            viewHolder.ct_name.setText(entity.getFoodname());
            viewHolder.ct_name2.setText("￥"+entity.getPrice());
            Picasso.with(context).load(Baseurl+entity.getPic()).into(viewHolder.ct_pic);

            viewHolder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context, FoodInfoActivity.class);
                    String shop_id=datalist.get(i).getShop_id();
                    String food_id=datalist.get(i).getFood_id();
                    String shopname=datalist.get(i).getShopname();
                    intent.putExtra("shop_id",shop_id);
                    intent.putExtra("food_id",food_id);
                    intent.putExtra("shop_name",shopname);
                    context.startActivity(intent);
                }
            });
            viewHolder.collect_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    uncollectFood(entity.getFood_id());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ct_pic;
        TextView ct_name;
        TextView ct_name2;
        Button collect_enter,collect_cancel;
        public ViewHolder(View v) {
            super(v);
            ct_pic=(ImageView)v.findViewById(R.id.ct_pic);
            ct_name=(TextView)v.findViewById(R.id.ct_name);
            ct_name2=(TextView)v.findViewById(R.id.ct_name2);
            collect_enter=(Button)v.findViewById(R.id.collect_enter);
            collect_cancel=(Button)v.findViewById(R.id.collect_cancel);
        }
    }
    public void uncollectShop(String Shop_id){
        this.shop_id=Shop_id;
        UserModel userModel=new UserModel();
        userModel.userCollectShop(userid,shop_id,this);
    }
    public void uncollectFood(String Food_id){
        this.food_id=Food_id;
        UserModel userModel=new UserModel();
        userModel.userCollectFood(userid,food_id,this);
    }
    @Override
    public void onSuccess(ResultBean object, int flag) {
        if (flag==2){
            Toast.makeText(context,"取消收藏店铺成功",Toast.LENGTH_SHORT).show();
            for (int i=0;i<datalist.size();i++){
                CollectInfoBean collectInfoBean=(CollectInfoBean)datalist.get(i);
                if (collectInfoBean.getShop_id().equals(shop_id)){
                    datalist.remove(collectInfoBean);
                    notifyDataSetChanged();
                    break;
                }
            }
        }
        if (flag==4){
            Toast.makeText(context,"取消收藏菜谱成功",Toast.LENGTH_SHORT).show();
            for (int i=0;i<datalist.size();i++){
                CollectInfoBean collectInfoBean=(CollectInfoBean)datalist.get(i);
                if (collectInfoBean.getFood_id().equals(food_id)){
                    datalist.remove(collectInfoBean);
                    notifyDataSetChanged();
                    break;
        }
    }
        }
    }

    @Override
    public void onFalure(String msg) {

    }
}
